package com.py.credit.service.impl;

import com.py.credit.dao.BigScreenForBlackAndRedDao;
import com.py.credit.dao.BigScreenSYDao;
import com.py.credit.service.BigScreenService;
import com.py.credit.utils.ThreadMapCallable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author ·︿·   LXW$
 * @title: BigScreenServiceImpl
 * @projectName credit-framework-common
 * @description:
 * @date 2020/7/2820:25
 */
@Service
public class BigScreenServiceImpl implements BigScreenService {
    @Autowired
    private BigScreenSYDao bigScreenSYDao;

    @Override
    public Map<String, Object> selectBatchSCount() {
        Map<String, Object> map =new HashMap<>();
        map.put("allCount",bigScreenSYDao.selectBatchSCount());
        map.put("frcount",bigScreenSYDao.selectCountFr());
        return map;
    }

    @Override
    public void getFrolonAndlat(String url, String mapKey) throws InterruptedException, ExecutionException {
        if (url == null || "".equals(url)) {
            url = "http://api.tianditu.gov.cn/geocoder";
        }
        long aa=System.currentTimeMillis();
        List<Map> listOfEmptyAdd = bigScreenSYDao.queryListOfEmpty();
        List<Map> target =new ArrayList<Map>();
        long bb=System.currentTimeMillis();
        int poolSize=50;
        ExecutorService es = Executors.newFixedThreadPool(poolSize);
        List<Future<List<Map<String,String>>>> listFuture = new ArrayList<Future<List<Map<String,String>>>>();
        List<List<Map>> listE=averageAssign(listOfEmptyAdd, poolSize);
        for (int i = 0; i < poolSize; i++) {
            Callable c = new ThreadMapCallable(listE.get(i),url,mapKey);
            Future<List<Map<String,String>>> f = es.submit(c);
            listFuture.add(f);
        }
        es.shutdown();
        long b=System.currentTimeMillis();
        for (Future f : listFuture) {
            // 从Future对象上获取任务的返回值，并输出到控制台
            List<Map>  lm= (List<Map>) f.get();
            target.addAll(lm);
        }
        long a=System.currentTimeMillis();
        //target.addAll((List<Map>)call(listOfEmptyAdd,url,mapKey));
        if(target!=null&&target.size()>0){
            bigScreenSYDao.updateEmptyAdd(target);
        }
    }


    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = source.size() % n; // (先计算出余数)
        int number = source.size() / n; // 然后是商
        int offset = 0;// 偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }


}
